package com.itqf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/10/12
 * @Time: 上午9:30
 */
@Controller
public class UploadController {

    //        <input type="file" name="photo">
    //MultipartFile photo  上传的文件
    @RequestMapping("/upload")
    @ResponseBody
    public Map<String,Object>  upload(String username, MultipartFile photo, HttpServletRequest request){
        //文件名称   girl.jpg
        String fileName =  photo.getOriginalFilename();
        //String path = request.getServletContext().getRealPath("images");
        String path = "/Users/liliting/Desktop/code/part3/SpringMVCDemo03/src/main/webapp/images";
        File dir = new File(path);
        if (!dir.exists()){
            dir.mkdir();
        }
        //得到文件的后缀
        String suffix = fileName.substring(fileName.indexOf("."));
        String newFileName = UUID.randomUUID().toString()+suffix;
        //hdihieofhoeifhoeifh390u0.jpg

        Map<String,Object> map = new HashMap<>();

        try {
            //把文件传输到本地
            photo.transferTo(new File(dir,newFileName));
            System.out.println("传成功，位置"+dir+"----username="+username);
            map.put("code",1);
            map.put("msg","上传成功！");
        } catch (IOException e) {
            e.printStackTrace();
            map.put("code",0);
            map.put("msg","上传失败！");
        }


        return  map;
    }

    //MultipartFile photo  上传的文件   多文件
    @RequestMapping("/upload2")
    @ResponseBody
    public Map<String,Object>  upload2(String username, MultipartFile [] photo, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();

        if (photo!=null&&photo.length>0){
            for (int i = 0; i < photo.length; i++) {
                //文件名称   girl.jpg
                String fileName =  photo[i].getOriginalFilename();
                //String path = request.getServletContext().getRealPath("images");
                String path = "/Users/liliting/Desktop/code/part3/SpringMVCDemo03/src/main/webapp/images";
                File dir = new File(path);
                if (!dir.exists()){
                    dir.mkdir();//创建目录
                }
                //得到文件的后缀   hdiwhdwi.diwdhi.jpg
                String suffix = fileName.substring(fileName.lastIndexOf("."));
                String newFileName = UUID.randomUUID().toString()+suffix;
                //hdihieofhoeifhoeifh390u0.jpg
                try {
                    //把文件传输到本地
                    //new File(dir,newFileName)  文件
                    photo[i].transferTo(new File(dir,newFileName));
                    System.out.println("传成功，位置"+dir+"----username="+username);
                    map.put("code",1);
                    map.put("msg","上传成功！");
                } catch (IOException e) {
                    e.printStackTrace();
                    map.put("code",0);
                    map.put("msg","上传失败！");
                }

            }
        }

        return  map;
    }

}
